<template>
  <el-drawer
    :title="title"
    :visible.sync="dialogFormVisible"
    size="500px"
    @close="close"
  >
    <el-form ref="formRef" :model="formData" :rules="rules" label-width="120px">
      <el-form-item label="学生姓名" prop="name">
        <el-input v-model.trim="formData.name" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="性别" prop="sex">
        <el-radio-group v-model="formData.sex">
          <el-radio-button label="0">男</el-radio-button>
          <el-radio-button label="1">女</el-radio-button>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="爸爸" prop="father">
        <el-input v-model.trim="formData.father" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="爸爸手机号" prop="fatherphone">
        <el-input
          v-model.trim="formData.fatherphone"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="妈妈" prop="mother">
        <el-input v-model.trim="formData.mother" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="妈妈手机号" prop="motherphone">
        <el-input
          v-model.trim="formData.motherphone"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="close">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </el-form-item>
    </el-form>
  </el-drawer>
</template>

<script>
  import { validateUsername, validatePhone } from '@/utils/validator'
  import { createStudent, updateStudent } from '@/apis/student'

  export default {
    name: 'StudentEditor',
    data() {
      return {
        formData: {
          name: null,
          sex: 0,
          father: null,
          fatherphone: null,
          mother: null,
          motherphone: null,
        },
        rules: {
          name: [
            { required: true, trigger: 'change', validator: validateUsername },
          ],
          sex: [{ required: true, trigger: 'change', message: '请选择性别' }],
          father: [{ trigger: 'change', validator: validateUsername }],
          fatherphone: [{ trigger: 'change', validator: validatePhone }],
          mother: [{ trigger: 'change', validator: validateUsername }],
          motherphone: [{ trigger: 'change', validator: validatePhone }],
        },
        title: '',
        dialogFormVisible: false,
      }
    },
    methods: {
      showEdit(row) {
        if (!row) {
          this.title = '添加'
        } else {
          this.title = '编辑'
          this.formData = Object.assign({}, row)
        }
        this.dialogFormVisible = true
      },
      close() {
        this.$refs['formRef'].resetFields()
        this.formData = this.$options.data().formData
        this.dialogFormVisible = false
      },
      save() {
        this.$refs['formRef'].validate(async (valid) => {
          if (valid) {
            try {
              let actionFn

              const {
                id,
                name,
                sex,
                father,
                fatherphone,
                mother,
                motherphone,
              } = this.formData
              const data = {
                id,
                name,
                sex,
                father,
                fatherphone,
                mother,
                motherphone,
              }

              if (this.formData.id) {
                actionFn = updateStudent
              } else {
                actionFn = createStudent
              }
              await actionFn(data)

              this.$baseMessage('操作成功', 'success')
              this.$emit('on-success')
              this.close()
            } catch (e) {
              this.$baseMessage('操作失败', 'error')
            }
          } else {
            return false
          }
        })
      },
    },
  }
</script>
